<template>
    <div class="app-container">
        <div class="searchAll" slot="search">
            <el-form :model="queryParams" ref="queryForm" v-show="showSearch" label-width="68px">
                <el-row :gutter="20">
                    <el-col :span="6">
                        <el-form-item :label="$t('dictionary.dictName')" prop="dictName" size="small">
                            <el-input v-model="queryParams.dictName" :placeholder="$t('public.placeholder',{placeholder: $t('dictionary.dictName')})" clearable size="small" @keyup.enter.native="handleQuery" />
                        </el-form-item>
                    </el-col>

                    <el-col :span="6">
                        <el-form-item :label="$t('dictionary.dictType')" prop="dictType" size="small">
                            <el-input v-model="queryParams.dictType" :placeholder="$t('public.placeholder',{placeholder: $t('dictionary.dictType')})" clearable size="small" @keyup.enter.native="handleQuery" />
                        </el-form-item>
                    </el-col>

                    <el-col :span="6">
                        <el-form-item :label="$t('public.status')" prop="status" size="small">
                            <el-select v-model="queryParams.status" :placeholder="$t('public.placeholder',{placeholder: $t('dictionary.dictStatus')})" clearable size="small">
                                <el-option v-for="dict in dict.type.sys_normal_disable" :key="dict.value" :label="dict.label" :value="dict.value" />
                            </el-select>
                        </el-form-item>
                    </el-col>

                    <el-col :span="6">
                        <el-form-item :label="$t('public.createTime')" size="small">
                            <el-date-picker v-model="dateRange" size="small" style="width:100%" value-format="yyyy-MM-dd" type="daterange" autocomplete="off" range-separator="-" :start-placeholder="$t('public.startDate')" :end-placeholder="$t('public.endDate')"></el-date-picker>
                        </el-form-item>
                    </el-col>

                </el-row>
                <el-row>
                    <el-col :span="24" align="center">
                        <span class="searchAres searchAresHeng">
                            <el-button type="primary" icon="el-icon-search" size="mini" class="searchBtn" @click="handleQuery">{{ $t("public.query") }}</el-button>
                            <el-button icon="el-icon-refresh" class="resectBtn" size="mini" @click="resetQuery">{{$t('public.reset')}}</el-button>
                        </span>
                    </el-col>
                </el-row>
            </el-form>
        </div>

        <div class="tableList">
            <div class="insTitle">
                <span>{{ $t("systemTitle.dictManagement") }}</span>
            </div>
            <div class="btnGroup">
                <el-button type="primary" icon="el-icon-document-add" size="mini" @click="handleAdd" v-hasPermi="['masterdata:dict:add']">{{$t('public.add')}}</el-button>
                <el-button icon="el-icon-delete" size="mini" class="deleteitemBtn" :disabled="multiple" @click="handleDelete" v-hasPermi="['masterdata:dict:remove']">{{$t('public.delete')}}</el-button>
                <el-button class="exportitemBtn" size="mini" @click="handleExport" v-hasPermi="['masterdata:dict:export']"><img src="../../../assets/images/icons/daochu.png" alt=""> {{$t('public.export')}}</el-button>
                <el-button type="primary" icon="el-icon-refresh" size="mini" @click="handleRefreshCache" v-hasPermi="['system:dict:remove']">刷新缓存</el-button>
            </div>
            <el-table max-height="350" border stripe v-loading="loading" :data="typeList" @selection-change="handleSelectionChange">

                <el-table-column type="selection" width="55" align="center" />
                <el-table-column label="操作" fixed="left" align="center" width="100">
                    <template slot-scope="scope">
                        <div class="action-wrap">
                            <table-body-btn type="edit" @click.native="handleUpdate(scope.row)" v-hasPermi="['masterdata:dict:edit']" />
                            <table-body-btn type="delete" @click.native="handleDelete(scope.row)" v-hasPermi="['masterdata:dict:remove']" />
                        </div>
                    </template>
                </el-table-column>
                <el-table-column :label="$t('dictionary.dictCode')" align="center" prop="dictId" width="100" show-overflow-tooltip />
                <el-table-column :label="$t('dictionary.dictName')" align="center" prop="dictName" :show-overflow-tooltip="true" />
                <el-table-column :label="$t('dictionary.dictType')" align="center" :show-overflow-tooltip="true" width="220">
                    <template slot-scope="scope">
                        <router-link :to="'/system/dict-data/index/' + scope.row.dictId" class="link-type">
                            <span>{{ scope.row.dictType }}</span>
                        </router-link>
                    </template>
                </el-table-column>
                <el-table-column :label="$t('public.status')" align="center" prop="status">
                    <template slot-scope="scope">
                        <dict-tag :options="dict.type.sys_normal_disable" :value="scope.row.status" />
                    </template>
                </el-table-column>
                <el-table-column :label="$t('public.remark')" align="center" prop="remark" :show-overflow-tooltip="true" />
                <el-table-column :label="$t('public.createTime')" align="center" prop="createTime" width="180">
                    <template slot-scope="scope">
                        <span>{{ parseTime(scope.row.createTime) }}</span>
                    </template>
                </el-table-column>

            </el-table>

            <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" layout="total, sizes, prev, pager, next, jumper" />
        </div>

        <!-- 添加或修改参数配置对话框 -->
        <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body :fullscreen="fullscreen">
            <DialogTit :title="title" :fullscreen.sync="fullscreen"></DialogTit>
            <el-form ref="form" :model="form" :rules="rules" label-width="80px">
                <el-form-item :label="$t('dictionary.dictName')" prop="dictName" size="small">
                    <el-input v-model="form.dictName" :placeholder="$t('public.placeholder',{placeholder: $t('dictionary.dictName')})" />
                </el-form-item>
                <el-form-item :label="$t('dictionary.dictType')" prop="dictType" size="small">
                    <el-input v-model="form.dictType" :placeholder="$t('public.placeholder',{placeholder: $t('dictionary.dictType')})" />
                </el-form-item>
                <el-form-item :label="$t('public.status')" prop="status" size="small">
                    <el-radio-group v-model="form.status">
                        <el-radio v-for="dict in dict.type.sys_normal_disable" :key="dict.value" :label="dict.value">{{dict.label}}</el-radio>
                    </el-radio-group>
                </el-form-item>
                <el-form-item :label="$t('public.remark')" prop="remark" size="small">
                    <el-input v-model="form.remark" :placeholder="$t('public.placeholder',{placeholder: $t('public.remark')})"></el-input>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="cancel" icon="el-icon-close" size="small">{{$t('public.cancel')}}</el-button>
                <el-button type="primary" @click="submitForm" icon="el-icon-check" size="small">{{$t('public.define')}}</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
import {
    listType,
    getType,
    delType,
    addType,
    updateType,
    refreshCache,
} from '@/api/basic/dict/type'

export default {
    name: 'Dict',
    dicts: ['sys_normal_disable'],
    data() {
        return {
            // 遮罩层
            loading: true,
            // 选中数组
            ids: [],
            names: [],
            // 非单个禁用
            single: true,
            // 非多个禁用
            multiple: true,
            // 显示搜索条件
            showSearch: true,
            // 总条数
            total: 0,
            // 字典表格数据
            typeList: [],
            // 弹出层标题
            title: '',
            // 是否显示弹出层
            open: false,
            // 日期范围
            dateRange: [],
            // 查询参数
            queryParams: {
                pageNum: 1,
                pageSize: 20,
                dictName: undefined,
                dictType: undefined,
                status: undefined,
            },
            // 表单参数
            form: {},
            // 表单校验
            rules: {
                dictName: [
                    {
                        required: true,
                        message: '字典名称不能为空',
                        trigger: 'blur',
                    },
                ],
                dictType: [
                    {
                        required: true,
                        message: '字典类型不能为空',
                        trigger: 'blur',
                    },
                ],
            },
            fullscreen: false,
        }
    },
    created() {
        this.getList()
    },
    methods: {
        /** 查询字典类型列表 */
        getList() {
            this.loading = true
            listType(this.addDateRange(this.queryParams, this.dateRange)).then(
                (response) => {
                    this.typeList = response.rows
                    this.total = response.total
                    this.loading = false
                }
            )
        },
        // 取消按钮
        cancel() {
            this.open = false
            this.reset()
        },
        // 表单重置
        reset() {
            this.form = {
                dictId: undefined,
                dictName: undefined,
                dictType: undefined,
                status: '0',
                remark: undefined,
            }
            this.resetForm('form')
        },
        /** 搜索按钮操作 */
        handleQuery() {
            this.queryParams.pageNum = 1
            this.getList()
        },
        /** 重置按钮操作 */
        resetQuery() {
            this.dateRange = []
            this.resetForm('queryForm')
            this.handleQuery()
        },
        /** 新增按钮操作 */
        handleAdd() {
            this.reset()
            this.open = true
            this.title = '添加字典类型'
        },
        // 多选框选中数据
        handleSelectionChange(selection) {
            this.ids = selection.map((item) => item.dictId)
            this.names = selection.map((item) => item.dictName)
            this.single = selection.length != 1
            this.multiple = !selection.length
        },
        /** 修改按钮操作 */
        handleUpdate(row) {
            this.reset()
            const dictId = row.dictId || this.ids
            getType(dictId).then((response) => {
                this.form = response.data
                this.open = true
                this.title = '修改字典类型'
            })
        },
        /** 提交按钮 */
        submitForm: function () {
            this.$refs['form'].validate((valid) => {
                if (valid) {
                    if (this.form.dictId != undefined) {
                        updateType(this.form).then((response) => {
                            this.$modal.msgSuccess(
                                this.$t('tips.updateSuccess')
                            )
                            this.open = false
                            this.getList()
                        })
                    } else {
                        addType(this.form).then((response) => {
                            this.$modal.msgSuccess(this.$t('tips.addSuccess'))
                            this.open = false
                            this.getList()
                        })
                    }
                }
            })
        },
        /** 删除按钮操作 */
        handleDelete(row) {
            const dictIds = row.dictId || this.ids
            const dictNames = row.dictName || this.names
            this.$modal
                .confirm(
                    this.$t('public.preConfirm', {
                        key: this.$t('dictionary.dictName'),
                        value: dictNames,
                    })
                )
                .then(function () {
                    return delType(dictIds)
                })
                .then(() => {
                    this.getList()
                    this.$modal.msgSuccess(this.$t('tips.deleteSuccess'))
                })
                .catch(() => {})
        },
        /** 导出按钮操作 */
        handleExport() {
            this.download(
                'masterdata/dict/type/export',
                {
                    ...this.queryParams,
                },
                `type_${new Date().getTime()}.xlsx`
            )
        },
        /** 刷新缓存按钮操作 */
        handleRefreshCache() {
            refreshCache().then(() => {
                this.$modal.msgSuccess(this.$t('tips.refreshSuccess'))
            })
        },
    },
}
</script>
